﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class System_Admin_AdminEdit : System.Web.UI.Page
{
    protected int userID;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!int.TryParse(Request.QueryString["userid"], out userID)) 
            return;
        if (!IsPostBack)
            BindData();
    }

    protected void BindData()
    {
        MatchIns.Model.TAdmin admin = new MatchIns.BLL.TAdmin().GetModel(userID);
        if (admin == null) return;

        //绑定管理员信息
        username.Text = admin.UserName.Trim();
        nickName.Text = admin.NickName.Trim();
    }
    protected void save_Click(object sender, EventArgs e)
    {
        MatchIns.Model.TAdmin admin = new MatchIns.BLL.TAdmin().GetModel(userID);
        admin.UserName = username.Text.Trim();
        admin.NickName = nickName.Text;
        admin.AdminId = userID;
        //验证数据完整性
        errors.Items.Clear();
        if (admin.UserName.Length == 0)
            errors.Items.Add("填写用户名");
        if (pwd.Text.Trim() == confirmPwd.Text.Trim() && pwd.Text.Trim().Length != 0)
        {
            //Response.Write(pwd.Text);
            admin.Password = pwd.Text.Trim();
            admin.Password = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(admin.Password, "MD5");
            //Response.Write(admin.Password); return;
        }
        if (pwd.Text.Trim() != confirmPwd.Text.Trim())
            errors.Items.Add("两次密码输入不一致");

        if (errors.Items.Count != 0)
            return;
        new MatchIns.BLL.TAdmin().Update(admin);
        errors.Items.Add("用户信息修改成功");
        //username.Text = pwd.Text = confirmPwd.Text = nickName.Text = string.Empty;
    }

    protected void cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("AdminList.aspx");
    }
}
